<?php

namespace App\Controllers;

class Travel extends BaseController {

    public function getIndex() {
        // connect to the model 
        $places = new \App\Models\Places();
        // retrieve all the records 
        $records = $places->findAll();
        $table = new \CodeIgniter\View\Table();
        // get a template parser 
        $parser = \Config\Services::parser();
        // tell it about the substitions 
        $headings = $places->fields;
        $displayHeadings = array_slice($headings, 1, 2);
        $table->setHeading(array_map('ucfirst', $displayHeadings));
        foreach ($records as $record) {
            
            $nameLink = anchor("travel/showme/$record->id", $record->name);
            $table->addRow($nameLink, $record->description);
            $template = [
                'table_open' => '<table cellpadding="5px">',
                'cell_start' => '<td style="border: 1px solid #dddddd;">',
                'row_alt_start' => '<tr style="background-color:#dddddd">',
            ];
            $table->setTemplate($template);
            $fields = [
                'title' => 'Travel Destinations',
                'heading' => 'Travel Destinations',
                'footer' => 'Copyright Xavier'
            ];
        }
        return $parser->setData($fields)
                        ->render('templates\top') .
                $table->generate() .
                        $parser->setData($fields)
                        ->render('templates\bottom');

        return $parser->setData(['records' => $records])
                        // and have it render the template with those 
                        ->render('placeslist');
    }

    public function getShowme($id) {
     $places = new \App\Models\Places();
        // retrieve all the records
        $record = $places->find($id);
        // get a template parser
        $parser = \Config\Services::parser();
        // tell it about the substitions
        $table = new \CodeIgniter\View\Table();
        $table->addRow("id",$record['id']);
        $table->addRow("name",$record['name']);
        $table->addRow("description",$record['description']);
        $table->addRow("link",$record['link']);
        $table->addRow("image",'<img src="/image/'.$record['image'].'"/>');
        $template = [
                'table_open' => '<table cellpadding="5px">',
                'cell_start' => '<td style="border: 1px solid #dddddd;">',
                'row_alt_start' => '<tr style="background-color:#dddddd">',
            ];
            $table->setTemplate($template);
            $fields = [
                'title' => 'Travel Destinations',
                'heading' => 'Travel Destinations',
                'footer' => 'Copyright Xavier'
            ];
        return $parser->setData($fields)
                        ->render('templates\top') .
                $table->generate() .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }

}
